package com.ftg.learn.chapter11.javabase;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * java配置文件类
 *   通过java完成java类的组件化
 * @Configuration : 配置文件
 * @bean  : 修饰方法  把方法返回对象 组件化
 * @author 布衣
 */
@Configuration
public class JavaConfiguration {
    @Bean
    public SqlSessionFactory initSqlsessionFactory(){
         
        org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();

        config.setLogImpl(Slf4jImpl.class);
        config.setLazyLoadingEnabled(true);
        //拦截器
        config.addInterceptor(new com.github.pagehelper.PageInterceptor());

        TransactionFactory transactionFactory = new JdbcTransactionFactory();

        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        dataSource.setURL("jdbc:mysql://localhost:3306/exam");

        Environment env = new Environment("dev", transactionFactory, dataSource);

        //给环境提供信息，用于连接数据 库
        config.setEnvironment(env);

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(config);
        sqlSessionFactory.getConfiguration().addMapper(Dept.class);
        return sqlSessionFactory;
    }
}
